<template>
    <div class="container-fluid" style="background: #fff">
        <div class="row page-title">
            <div class="col-xs-11">
                <h1>绑定</h1>
            </div>
            <div class="col-xs-1">
                <a title="添加"  @click="dialogFormVisible = true">
                    <el-tooltip content="添加" placement="top">
                        <i class="el-icon-plus"></i>
                    </el-tooltip>
                </a>
            </div>
        </div>
        <el-row style="margin-bottom: 20px;">
            <el-col :span="24">
                <el-breadcrumb separator="/">
                    <el-breadcrumb-item :to="{ path: item.link }" v-for="item in breadcrumb" :key="item.name">{{item.name}}</el-breadcrumb-item>
                    <el-breadcrumb-item>绑定</el-breadcrumb-item>
                </el-breadcrumb>
            </el-col>
        </el-row>
        <el-col :span="24" class="table-bottom-margin">
            <el-table :data="tableData" border style="width: 100%" :header-cell-class-name="headerbg">
                <el-table-column prop="Title" label="绑定内容"></el-table-column>
                <el-table-column prop="Type" label="绑定类型"> </el-table-column>
                <el-table-column prop="SortNo" label="顺序"> </el-table-column>
                <el-table-column label="操作" align="center" min-width="40" width="250px">
                    <template slot-scope="scope">
                        <el-tooltip content="前移" placement="top">
                            <i class="fa fa-arrow-left el-text-yellow" @click="order(scope.row.Id,(Number(scope.row.SortNo)-1))"></i>
                        </el-tooltip>&nbsp;&nbsp;&nbsp;&nbsp;
                        <el-tooltip content="后移" placement="top">
                            <i class="fa fa-arrow-right el-text-yellow" @click="order(scope.row.Id,(Number(scope.row.SortNo)+1))"></i>
                        </el-tooltip>&nbsp;&nbsp;&nbsp;&nbsp;    
                        <el-tooltip content="进入视频" placement="top">
                            <i class="fa fa-edit el-text-blue" @click="Edit(scope.row)"></i>
                        </el-tooltip>&nbsp;&nbsp;&nbsp;&nbsp;            
                        <el-tooltip content="删除" placement="top">
                            <i class="fa fa-trash el-text-danger" @click="del(scope.row.Id)"></i>
                        </el-tooltip>                                                        
                    </template>
                </el-table-column>
            </el-table>    
        </el-col>
        <el-col :sapn="24" style="text-align:right" v-if="count>0">
            <el-pagination @size-change="pageSizeChange" @current-change="pageCurrentChange" :current-page="currentPage"
                :page-sizes="[10]" :page-size="pageSize" layout="total, sizes, prev, pager, next, jumper" :total="count">
            </el-pagination>
        </el-col>
        <br/>
        <!--添加/编辑-->
        <el-dialog title="新增绑定" :visible.sync="dialogFormVisible">
            <el-form :model="form" ref="form" :rules="rules">
                <el-form-item class="hr" :label-width="formLabelWidth"></el-form-item>
                 <el-form-item label="跳转位置：" prop="VideoId" :label-width="formLabelWidth">
                    <el-select v-model="form.VideoId" filterable remote reserve-keyword placeholder="请输入关键词" :remote-method="remoteMethod" :loading="loading" clearable style="width:60%;">
                        <el-option v-for="item in searchedSelectList" :key="item.value" :label="item.label" :value="item.value"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="展示顺序：" prop="SortNo" :label-width="formLabelWidth">
                    <el-input v-model.number="form.SortNo" type="number" placeholder="输入数字，数字越小越展示优先" style="width:60%;"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="save" size="medium">确 定</el-button>
                <el-button @click="dialogFormVisible = false" size="medium">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        data(){
            return{
                currentPage:1,
                pageSize:10,
                count:0,
                headerbg:'headerbg',//表格头部背景色
                dialogFormVisible: false,
                formLabelWidth: '25%',
                tableData:[],
                form: {},
                rules:{
                    VideoId:[
                        { required: true, message: '请选择跳转位置', trigger: 'change' }
                    ],
                    SortNo:[
                        { required: true, message: '请输入展示顺序'},
                        { type: 'number', message: '顺序必须为小于51的正整数', min:0 ,max:50}
                    ]
                },
                getListUrl:this.urlConfig+'/api/ClassManagement/GetClassbandList',
                delUrl:this.urlConfig+'/api/ClassManagement/PostDeleteClassband',
                editUrl:this.urlConfig+'/api/ClassManagement/PostEditClassband',
                addUrl:this.urlConfig+'/api/ClassManagement/PostClassband',
                DropDownListUrl:this.urlConfig+'/api/AdvertInfo/GetVideoDropList',
                    
                searchedSelectList: [],
                dropDownList: [],
                loading: false,

                // subId: $route.query.subId,//上级id
            }
        },
        components: {
        },
        mounted() {
            this.getList(10,this.currentPage);
            this.getDropDownList();
        },
        watch:{
        },
        computed:{
            breadcrumb(){
                var breadcrumbArr = [];
                var linkUrl = '/SortManagement/Sort?ClassManagementId=';
                breadcrumbArr=this.$route.query.breadcrumb.map((currentValue, index,arr)=>{
                    if(index==1){
                        linkUrl +=this.$route.query.id;
                    }
                    return {link: linkUrl, name: currentValue};
                });
                return breadcrumbArr;
            }
        },
        methods:{
            remoteMethod(query) {
                if (query !== '') {
                    this.loading = true;
                    setTimeout(() => {
                        this.loading = false;
                        this.searchedSelectList = this.dropDownList.filter(item => {
                            return item.label.indexOf(query) > -1;
                        });
                    }, 200);
                } else {
                    this.searchedSelectList = [];
                }
            },
            getDropDownList:function(){
                this.$http.get(this.DropDownListUrl,{
                    emulateJSON:true
                }).then(function(res){
                    if(res.body["Code"] == "200") {
                        this.dropDownList = JSON.parse(res.body.Data);
                        this.searchedSelectList = this.dropDownList;
                    }
                });
            },
            getList:function(PageSize,PageIndex){
                this.$http.get(this.getListUrl+'?PageSize='+PageSize+'&PageIndex='+PageIndex+'&Id='+((this.$route.query.NameType)?this.$route.query.subId:this.$route.query.id)+'&BandType='+this.$route.query.NameType,{
                    emulateJSON:true
                }).then(function(res){
                    this.count = 0;
                    this.tableData = [];
                    if(res.body["Code"] == "200") {
                        this.count = res.body.Count;
                        this.tableData = JSON.parse(res.body.Data);
                    }
                });
            },
            Edit:function(row){
                //跳转多视频页面 this.$route.query.id
                var LinkUrl = '';
                if(row.Type='多视频'){
                    LinkUrl = '/Video/EditMultiVideo';
                }else if(row.Type=='单视频'){
                    LinkUrl = '/Video/EditSingleVideo';
                }
                this.$router.push({ path: LinkUrl ,query: { id: row.VideoId} });
            },
            del:function(id){
                this.$confirm('确定需要删除该记录?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http.post(this.delUrl+'?Id='+id,{
                        emulateJSON:true
                    }).then(function(res){
                        if(res.body["Code"] == "200") {
                            this.getList(10,this.currentPage);
                            this.$notify({title: '成功',message: '删除成功！',type: 'success'});
                        }else{
                            this.$notify({title: '警告',message: res.body["Message"],type: 'warning',duration:1200});
                        }
                    });
                }).catch(() => {});
            },
            order:function(Id,order){
                this.$http.post(this.editUrl,{Id:Id,SortNo:order,BandType:this.$route.query.NameType},{
                    emulateJSON: true
                }).then(function(res) {
                    console.log(res,'res');
                    if(res.body["Code"] == "200") {
                        this.getList(10,this.currentPage);
                    }else{
                        this.$notify.error({title: '错误',message:res.body["Message"]});
                    }
                });
            },
            validateFun:function(){
                var errorInfo='';
                for(var i=0;i<2;i++){
                    this.$refs.form.validateField((['VideoId','SortNo'][i]),(re)=>{
                        errorInfo+=re+(re==''?'':', ');
                    })
                }
                return errorInfo;
            },
            save:function(){
                var errorInfo=this.validateFun();
                if(!errorInfo){
                    if(this.$route.query.NameType){
                        this.form.SubClassManagementId = this.$route.query.subId;//二级
                    }
                    this.form.ClassManagementId = this.$route.query.id;
                    this.form.BandType = this.$route.query.NameType;
                    this.$http.post(this.addUrl,this.form,{
                        emulateJSON: true
                    }).then(function(res) {
                        console.log(res.body["Code"],'save')
                        if(res.body["Code"] == "200") {
                            this.dialogFormVisible = false;
                            this.getList(10,this.currentPage);
                        }else{
                            this.$notify.error({title: '错误',message:res.body["Message"]});
                        }
                    });
                }else{
                    this.$notify({title: '警告',message: errorInfo,type: 'warning',duration:1200});
                    return false;
                }
            },
            pageSizeChange:function(val){
                this.pageSize = val;
                this.getList(val,this.currentPage);
            },
            pageCurrentChange:function(val){
                this.currentPage = val;
                this.getList(this.pageSize,val);
            }
        }
    }
</script>
<style>
    .headerbg{
        background-color: #f4f3f3 !important;
    }
</style>
